
// 定义一个handle类
class handle {
	constructor(args) {
		return args;
	}
	/*** 添加进购物车
	*@param  {object}  goods 商品
	结构如下:
	  {product_id:?,norms_value_id:?,store_id:?,store_name:?,product_name:?,norms_name:?,price:?,number:?,src:?}
	*@param  {object}  cartlist  购物车列表集合 
	结构如下:
	storeid:{
		name:'xxxxx门店',
		goodslist:{
			norms_value_id:{
				product_id,
				store_id:,
				name:'xxx产品',
				norms_name:'金色,大尺码',
				price:89.00,
				number:2,
				src:''
			}
		}
	}
	*@param  {callback} 添加成功，回调
	@return {object}  cartlist 购物车列表集合
	***/
	add(goods,cartList,callback){
		try{
			let data = {statu:1,code:0,data:[],msg:'成功'}
			//购物车里门店的商品
			if(cartList[goods.store_id] instanceof Object)
			{
				//是否已有产品
				if(cartList[goods.store_id].goodslist[goods.norms_value_id] instanceof Object)
				{
					cartList[goods.store_id].goodslist[goods.norms_value_id].number = cartList[goods.store_id].goodslist[goods.norms_value_id].number+goods.number;
				}else{
					cartList[goods.store_id].goodslist[goods.norms_value_id] =  {
																				  product_id:goods.product_id,
																				  store_id:goods.store_id,
																				  name:goods.product_name,
																				  norms_name:goods.norms_name,				
																				  price:goods.price,
																				  number:goods.number,
																				  src:goods.src,
																				  check:false,
																				  multiple:goods.multiple||1,
																				}
																		
				}
				
			}else{
			
				cartList[goods.store_id]={
											name:goods.store_name,
											goodslist:{},
											check:false
										}
									
				cartList[goods.store_id].goodslist[goods.norms_value_id] =  {
																			  product_id:goods.product_id,
																			  store_id:goods.store_id,
																			  name:goods.product_name,
																			  norms_name:goods.norms_name,				
																			  price:goods.price,
																			  number:goods.number,
																			  src:goods.src,
																			  check:false,
																				multiple:goods.multiple||1,
																			}
			}
			data.data.cartList = cartList
			callback(data)
			// return cartList
		}catch(exception){
			console.log(exception)
			return false
		}
		
	}


	/*** 减购物车商品数量
	*@param  {object}  goods 商品
	结构如下:
	  {norms_value_id:?,store_id:?}
	*@param  {object}  cartlist  购物车列表集合 
	结构如下:
	storeid:{
		name:'xxxxx门店',
		goodslist:{
			norms_value_id:{
				product_id,
				store_id:,
				name:'xxx产品',
				norms_name:'金色,大尺码',
				price:89.00,
				number:2,
				src:''
			}
		}
	}
	*@param  {callback} 回调
	@return {object}  cartlist 购物车列表集合
	***/
	desc(goods,cartList,callback,multiple=1) {
		try{
			let data = {statu:1,code:0,data:{},msg:'成功'}
			if(cartList[goods.store_id].goodslist[goods.norms_value_id].number>1){
				cartList[goods.store_id].goodslist[goods.norms_value_id].number= cartList[goods.store_id].goodslist[goods.norms_value_id].number -multiple
			}else{
				
				if(Object.keys(cartList[goods.store_id].goodslist).length>1)
				{
					
					Reflect.deleteProperty(cartList[goods.store_id].goodslist,goods.norms_value_id)
				}else{
				
					Reflect.deleteProperty(cartList,goods.store_id)
				}
				
			}
			data.data.cartList = cartList
			callback(data)
			return cartList
		}catch(exception){
			console.log(exception)
			return false
		}
		
	}
	
	
	/*** 删除预选
	*@param  {object}  goods 商品
	结构如下:
	  {norms_value_id:?,store_id:?}
	*@param  {object}  cartlist  购物车列表集合 
	结构如下:
	storeid:{
		name:'xxxxx门店',
		goodslist:{
			norms_value_id:{
				product_id,
				store_id:,
				name:'xxx产品',
				norms_name:'金色,大尺码',
				price:89.00,
				number:2,
				src:''
			}
		}
	}
	*@param  {callback} 回调
	@return {object}  cartlist 购物车列表集合
	***/
	delProd(goods,cartList,callback) {
		try{
			let data = {statu:1,code:0,data:{},msg:'成功'}
		
				
			if(Object.keys(cartList[goods.store_id].goodslist).length>1)
			{
				
				Reflect.deleteProperty(cartList[goods.store_id].goodslist,goods.norms_value_id)
			}else{
			
				Reflect.deleteProperty(cartList,goods.store_id)
			}
				
			
			data.data.cartList = cartList
			callback(data)
			return cartList
		}catch(exception){
			console.log(exception)
			return false
		}
		
	}
	
	
}
export default new handle();
